package com.javaee.spring.chineseIdol;

import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@Aspect
public class Audience {
	@Pointcut("execution(* com.javaee.spring.chineseIdol.Performer.performer(..))")
	public void performers(){
		
	}
	@Before("performers()")
	public void takeSeat(){
		System.out.println("观众入座!");
	}
	@Before("performers()")
	public void turnOffCellPhones(){
		System.out.println("观众关闭手机!");
	}
	@AfterReturning("performers()")
	public void applaud(){
		System.out.println("观众鼓掌! 👏👏👏");
	}
	
	@AfterThrowing("performers()")
	public void demandRefund(){
		System.out.println("尼玛 退票!");
	}
	
	@Around("performers()")
	public void watchPerformance(ProceedingJoinPoint joinPoint){
		
		try {
			System.out.println("观众入座!!");
			System.out.println("观众关闭手机!!");
			long start = System.currentTimeMillis();
			joinPoint.proceed();
			long end = System.currentTimeMillis();
			System.out.println("观众鼓掌!! 👏👏👏");
			System.out.println("持续时间 "+(end-start)+ " 毫秒!");
		} catch (Throwable e) {
			System.out.println("尼玛 退票!!!");
		}
		
	}
	
	public static void main(String[] args) throws PerformanceException {
		ConfigurableApplicationContext ctx =
			      new ClassPathXmlApplicationContext("com/javaee/spring/chineseIdol/chinese-idol-aop2.xml");
		Performer performer = (Performer)ctx.getBean("tom");
		performer.performer();
		Contestant con = (Contestant)performer;
		con.receiveAward();
		ctx.close();
	}
}

